package com.salon.common.utils;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.Base64.Decoder;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import com.salon.common.exception.CustomException;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

public class AesEncryptUtils {

    private AesEncryptUtils() {
        throw new IllegalStateException("Utility class");
    }

    public static String encode(String aesKey, String content) {
        try {
            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            keygen.init(128, new SecureRandom(aesKey.getBytes()));
            SecretKey originalKey = keygen.generateKey();
            byte[] raw = originalKey.getEncoded();
            SecretKey key = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(1, key);
            byte[] byteEncode = content.getBytes(StandardCharsets.UTF_8);
            byte[] byteAes = cipher.doFinal(byteEncode);
            return Base64Utils.encode(byteAes);
        } catch (Exception var10) {
            var10.printStackTrace();
            return null;
        }
    }

    public static String decode(String aesKey, String encrypText) {
        try {
            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            keygen.init(128, new SecureRandom(aesKey.getBytes()));
            SecretKey originalKey = keygen.generateKey();
            byte[] raw = originalKey.getEncoded();
            SecretKey key = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(2, key);
            byte[] byteContent = Base64Utils.decodeFast(encrypText);
            byte[] byteDecode = cipher.doFinal(byteContent);
            return new String(byteDecode, StandardCharsets.UTF_8);
        } catch (Exception var10) {
            var10.printStackTrace();
            return null;
        }
    }

    public static String decodeRan(String aesKey, String encrypText) {
        try {
            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(aesKey.getBytes());
            keygen.init(128, random);
            SecretKey originalKey = keygen.generateKey();
            byte[] raw = originalKey.getEncoded();
            SecretKey key = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(2, key);
            byte[] byteContent = Base64Utils.decodeFast(encrypText);
            byte[] byteDecode = cipher.doFinal(byteContent);
            return new String(byteDecode, StandardCharsets.UTF_8);
        } catch (Exception var11) {
            var11.printStackTrace();
            return null;
        }
    }

    public static String encode(String aesKey, String content, String transformation, String iv) {
        try {
            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            keygen.init(128, new SecureRandom(aesKey.getBytes()));
            SecretKey originalKey = keygen.generateKey();
            byte[] raw = originalKey.getEncoded();
            SecretKey key = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance(transformation);
            if ("CBC/CFB/OFB".contains(transformation.substring(4, 7))) {
                cipher.init(1, key, new IvParameterSpec(iv.getBytes()));
            } else {
                cipher.init(1, key);
            }
            byte[] byteEncode = content.getBytes(StandardCharsets.UTF_8);
            byte[] byteAes = cipher.doFinal(byteEncode);
            return Base64Utils.encode(byteAes);
        } catch (Exception var12) {
            var12.printStackTrace();
            return null;
        }
    }

    public static String decode(String aesKey, String encrypText, String transformation,
            String iv) {
        try {
            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            keygen.init(128, new SecureRandom(aesKey.getBytes()));
            SecretKey originalKey = keygen.generateKey();
            byte[] raw = originalKey.getEncoded();
            SecretKey key = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance(transformation);
            if ("CBC/CFB/OFB".contains(transformation.substring(4, 7))) {
                cipher.init(2, key, new IvParameterSpec(iv.getBytes()));
            } else {
                cipher.init(2, key);
            }
            byte[] byteContent = Base64Utils.decodeFast(encrypText);
            byte[] byteDecode = cipher.doFinal(byteContent);
            return new String(byteDecode, StandardCharsets.UTF_8);
        } catch (Exception var12) {
            var12.printStackTrace();
            return null;
        }
    }

    public static String decodeForWechat(String encrypText, String aesKey, String transformation,
            String iv) {
        try {
            Decoder decoder = Base64.getDecoder();
            byte[] ivByte = decoder.decode(iv);
            byte[] keyByte = decoder.decode(aesKey);
            byte[] encryptedByte = decoder.decode(encrypText);
            SecretKeySpec skeySpec = new SecretKeySpec(keyByte, "AES");
            Cipher cipher = Cipher.getInstance(transformation);
            cipher.init(2, skeySpec, new IvParameterSpec(ivByte));
            byte[] byteDecode = cipher.doFinal(encryptedByte);
            return new String(byteDecode);
        } catch (Exception var12) {
            var12.printStackTrace();
            return null;
        }
    }

    /**
     * 密钥+内容加密 SQL方法 AES_DECRYPT(UNHEX(info),'123@456')
     *
     * @param content 加密内容
     * @param aesKey 密钥
     */
    public static String aesHexDecrypt(String content, String aesKey) {
        try {
            SecretKey key = generateAESKey(aesKey, "ASCII");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(2, key);
            byte[] cleartext = Hex.decodeHex(content.toCharArray());
            byte[] ciphertextBytes = cipher.doFinal(cleartext);
            return new String(ciphertextBytes, StandardCharsets.UTF_8);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | DecoderException var6) {
            var6.printStackTrace();
            return null;
        }
    }

    public static SecretKeySpec generateAESKey(final String key, final String encoding) {
        try {
            byte[] finalKey = new byte[16];
            int i = 0;
            byte[] var4 = key.getBytes(encoding);
            int var5 = var4.length;
            for (int var6 = 0; var6 < var5; ++var6) {
                byte b = var4[var6];
                int var10001 = i++;
                finalKey[var10001 % 16] ^= b;
            }
            return new SecretKeySpec(finalKey, "AES");
        } catch (UnsupportedEncodingException var8) {
            throw new CustomException("generateAESKey error");
        }
    }

    /**
     * hex+密钥解密 SQL方法 HEX(AES_ENCRYPT(info,'123@456'))
     *
     * @param password 密码
     * @param strKey 密钥
     */
    public static String aesHexEncrypt(String password, String strKey) {
        try {
            SecretKey key = generateAESKey(strKey, "ASCII");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(1, key);
            byte[] cleartext = password.getBytes(StandardCharsets.UTF_8);
            byte[] ciphertextBytes = cipher.doFinal(cleartext);
            return new String(Hex.encodeHex(ciphertextBytes));
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException var6) {
            var6.printStackTrace();
            return null;
        }
    }

}
